<template>
  <div class="history">
    <el-card class="history-card">
      <template #header>
        <div class="card-header">
          <span>历史记录</span>
          <el-button type="danger" @click="clearHistory" plain>清空</el-button>
        </div>
      </template>
      <el-scrollbar height="200px">
        <div v-for="(record, index) in history" :key="index" class="history-item">
          <span class="time">{{ record.time }}</span>
          <span class="result">结果: {{ record.results.join(', ') }}</span>
        </div>
      </el-scrollbar>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const history = ref([])

onMounted(() => {
  // 从 uTools 数据库加载历史记录
  history.value = window.diceDB.getHistory()
})

const addRecord = (results) => {
  const time = new Date().toLocaleTimeString()
  const newHistory = [{ time, results }, ...history.value]
  history.value = newHistory
  // 保存到 uTools 数据库
  window.diceDB.saveHistory(newHistory)
}

const clearHistory = () => {
  history.value = []
  // 清空 uTools 数据库中的历史记录
  window.diceDB.clearHistory()
}

defineExpose({ addRecord })
</script>

<style lang="scss" scoped>
.history {
  margin-top: 20px;
  
  .history-card {
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }

  .history-item {
    padding: 10px;
    border-bottom: 1px solid #eee;
    display: flex;
    justify-content: space-between;
    
    &:last-child {
      border-bottom: none;
    }

    .time {
      color: #999;
      font-size: 14px;
    }

    .result {
      font-weight: bold;
    }
  }
}
</style> 